package com.example.huichaoliu;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.huichaoliu.app.R;
import com.huichaoliu.data.Config;
import com.huichaoliu.data.UserInfo;
import com.rederxu.AsyncTask.ImageTask;
import com.rederxu.farmer.BaseActivity;
import com.rederxu.request.HTTPRequest;
import com.rederxu.request.onRequestOverListener;
import com.source.utils.Utils;

public class PersonalDataActivity extends BaseActivity implements
		OnClickListener {

	private Spinner mSpinner = null;
	private TextView tv_sex = null;
	private TextView tv_constellation = null;
	private TextView tv_area = null;
	private ImageView ic_avatar = null;
	private TextView tv_name = null;
	private TextView tv_sign = null;
	private TextView tv_hobby = null;
	private TextView tv_job = null;
	private String[] sex = { "男", "女" };
	private String[] constellation = { "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座",
			"处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座" };
	private String[] area = { "北京", "天津", "上海", "重庆", "河北", "山西", "辽宁", "吉林",
			"黑龙江", "江苏", "浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东",
			"海南", "四川", "贵州", "云南", "陕西", "甘肃", "青海", "台湾", "内蒙古", "广西", "西藏",
			"宁夏", "新疆", "香港", "澳门", "其他" };
	private final int CODE_SEX = 1001;
	private final int CODE_CONSTELLATION = 1002;
	private final int CODE_AREA = 1003;
	private final int CODE_BACK_FROM_NAME = 100;
	private final int CODE_BACK_FROM_SIGN = 101;
	private final int CODE_BACK_FROM_HOBBY = 102;
	private final int CODE_BACK_FROM_JOB = 103;
	private int type_now = 0;
	private boolean isFirst = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_personal_data);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.left_button:
			finish();
			break;
		case R.id.layout_receipt_address:
			startActivity(ReceiptAddressActivity.class);
			break;
		case R.id.layout_name:
			startActivityForResult(NameInputActivity.class, CODE_BACK_FROM_NAME);
			break;
		case R.id.layout_job:
			startActivityForResult(JobInputActivity.class, CODE_BACK_FROM_JOB);
			break;
		case R.id.layout_hobby:
			startActivityForResult(PreferInputActivity.class,
					CODE_BACK_FROM_HOBBY);
			break;
		case R.id.layout_sign:
			startActivityForResult(SignInputActivity.class, CODE_BACK_FROM_SIGN);
			break;
		case R.id.layout_sex:
			setSpinner(CODE_SEX);
			break;
		case R.id.layout_constellation:
			setSpinner(CODE_CONSTELLATION);
			break;
		case R.id.layout_area:
			setSpinner(CODE_AREA);
			break;
		}
	}

	@Override
	public void init() {
		mSpinner = (Spinner) findViewById(R.id.mSpinner);
		((TextView) findViewById(R.id.title_text)).setText("个人资料");
		findViewById(R.id.left_button).setOnClickListener(this);
		findViewById(R.id.layout_receipt_address).setOnClickListener(this);
		findViewById(R.id.layout_sex).setOnClickListener(this);
		findViewById(R.id.layout_constellation).setOnClickListener(this);
		findViewById(R.id.layout_name).setOnClickListener(this);
		findViewById(R.id.layout_area).setOnClickListener(this);
		findViewById(R.id.layout_job).setOnClickListener(this);
		findViewById(R.id.layout_sign).setOnClickListener(this);
		findViewById(R.id.layout_hobby).setOnClickListener(this);

		tv_sex = (TextView) findViewById(R.id.tv_sex);
		tv_area = (TextView) findViewById(R.id.tv_area);
		tv_name = (TextView) findViewById(R.id.tv_name);
		tv_sign = (TextView) findViewById(R.id.tv_sign);
		tv_hobby = (TextView) findViewById(R.id.tv_hobby);
		tv_job = (TextView) findViewById(R.id.tv_job);
		ic_avatar = (ImageView) findViewById(R.id.ic_avatar);
		tv_constellation = (TextView) findViewById(R.id.tv_constellation);
	}

	@Override
	public void initData() {
		fillData();
		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				if (isFirst) {
					isFirst = false;
					return;
				}
				String param = "sex";
				String value = "男";
				if (type_now == CODE_SEX) {
					param = "sex";
					value = sex[position];
					tv_sex.setText(value);
				} else if (type_now == CODE_CONSTELLATION) {
					param = "constellation";
					value = constellation[position];
					tv_constellation.setText(constellation[position]);
				} else if (type_now == CODE_AREA) {
					param = "area";
					value = area[position];
					tv_area.setText(area[position]);
				}

				try {
					JSONObject jo = new JSONObject();
					jo.put(param, value);
					jo.put("userId", Config.getUserId(mContext));
					Bundle bundle = new Bundle();
					bundle.putString("request", Utils.requestParamsEncode(jo));
					HTTPRequest.POST(mContext,
							Config.REQUEST_URLS.URL_USER_UPDATE_INFO, bundle,
							new onRequestOverListener() {
								@Override
								public void onRequestOver(String result) {
									try {
										JSONObject jo = new JSONObject(result);
										if (jo.getInt("code") != 0) {
											showToast(jo.getString("message"));
											return;
										} else {
											showToast(jo.getString("message"));
											jo = jo.getJSONObject("response");
											UserInfo.initData(jo, mContext);
											return;
										}
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void fillData() {
		try {
			UserInfo info = new UserInfo(mContext);
			String avatar = info.getAvatar();
			if (avatar != null) {
				ImageTask task = new ImageTask(ic_avatar,
						Config.getUserId(mContext), null);
				task.execute(avatar);
				Log.e("avatar", avatar);
			}
			if (info.getNickname() != null && !info.getNickname().equalsIgnoreCase("null")) {
				tv_name.setText(info.getNickname());
			}
			if (info.getSignature() != null && !info.getSignature().equalsIgnoreCase("null")) {
				tv_sign.setText(info.getSignature());
			}
			if (info.getHobby() != null && !info.getHobby().equalsIgnoreCase("null")) {
				tv_hobby.setText(info.getHobby());
			}
			if (info.getSex() != null && !info.getSex().equalsIgnoreCase("null")) {
				tv_sex.setText(info.getConstellation());
			}
			if (info.getOccupation() != null && !info.getOccupation().equalsIgnoreCase("null")) {
				tv_job.setText(info.getOccupation());
			}
			if (info.getConstellation() != null && !info.getConstellation().equalsIgnoreCase("null")) {
				tv_constellation.setText(info.getConstellation());
			}
			if (info.getArea() != null && !info.getArea().equalsIgnoreCase("null")) {
				tv_area.setText(info.getArea());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setSpinner(int type) {
		isFirst = true;
		type_now = type;
		String[] data = sex;
		if (type_now == CODE_SEX) {
			data = sex;
		} else if (type_now == CODE_CONSTELLATION) {
			data = constellation;
		} else if (type_now == CODE_AREA) {
			data = area;
		}
		ArrayAdapter adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, data);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinner.setAdapter(adapter);
		mSpinner.performClick();
	}

	@Override
	public void onNetworkChanged(boolean isMobNetConnected,
			boolean isWifiConnected) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (RESULT_OK == RESULT_OK) {
			fillData();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
